﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SQLite;

namespace SqliteWrapper.Procedures
{
    public class Proc_BatchInsertBooks : IProcedure
    {
        public override SqliteError doProcedure(object DataSource, string DBPath)
        {
            List<Book> bkList = DataSource as List<Book>;
            SqliteError error = new SqliteError() { ErrorText = null, IsProcessSuccess = false };
            SqliteConnectionManager manager = SqliteConnectionManager.Create(DBPath);
            using (SQLiteWriteLock writeLock = new SQLiteWriteLock(manager.ConnectionString))
            {
                using (SQLiteConnection conn = manager.GetSqliteConnection())
                {
                    conn.Open();
                    using (SQLiteTransaction trans = conn.BeginTransaction())
                    {
                        bkList.ForEach(book =>
                            {
                                using (SQLiteCommand cmd = conn.CreateCommand())
                                {
                                    cmd.CommandText = "insert into Book(Title,SourcePath,AlbumId,CategoryId,CreateTimeTag,Desc,Thumbnail) " +
                                                      "values(@Title,@SourcePath,@AlbumId,@CategoryId,@CreateTimeTag,@Desc,@Thumbnail)";
                                    cmd.Parameters.Add("@Title", System.Data.DbType.String).Value = book.Title;
                                    cmd.Parameters.Add("@SourcePath", System.Data.DbType.String).Value = book.SourcePath;
                                    cmd.Parameters.Add("@AlbumId", System.Data.DbType.Int64).Value = book.AlbumId;
                                    cmd.Parameters.Add("@CategoryId", System.Data.DbType.Int64).Value = book.CategoryId;
                                    cmd.Parameters.Add("@CreateTimeTag", System.Data.DbType.String).Value = book.CreateTimeTag;
                                    cmd.Parameters.Add("@Desc", System.Data.DbType.String).Value = book.Desc;
                                    cmd.Parameters.Add("@Thumbnail", System.Data.DbType.String).Value = book.Thumbnail;
                                    cmd.Transaction = trans;
                                    cmd.CommandType = System.Data.CommandType.Text;
                                    int testHandle = cmd.ExecuteNonQuery();
                                }
                            });
                        try
                        {
                            trans.Commit();
                            error.IsProcessSuccess = true;
                        }
                        catch (Exception ex)
                        {
                            error.ErrorText = ex.Message;
                            trans.Rollback();
                        }
                    }
                }
            }
            return error;
        }
    }
}
